package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.UserRoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.UserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;


/**
 * @author ：lizimo
 * @date ：Created in 2020/1/2
 * @description ：角色实现类
 * @version: 1.0
 */
@Slf4j
@Service
public class UserRoleServiceImpl implements UserRoleService {

    @Autowired
    private UserRoleDao userRoleDao;



    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[角色-分页查询]data:{}",queryPageBean);
        //设置分页参数
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
         //查询Page
        Page<Role>  page = userRoleDao.selectByCondition(queryPageBean.getQueryString());
        //封装返回值
        return new  PageResult(page.getTotal(),page.getResult());

    }

    @Override
    public Role findById(Integer id){
        log.info("[角色-根据id查询]");
        return userRoleDao.selectById(id);
    }






    @Override
    public void add(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("[角色-添加]data:{},permissionIds:{},menuIds:{}", role, permissionIds, menuIds);
        //插入基本数据
        userRoleDao.insert(role);

        if (menuIds.length > 0 || permissionIds.length > 0) {
            for (Integer permissionId : permissionIds) {
                userRoleDao.insertRoleAndPermissionId(role.getId(), permissionId);
            }

            for (Integer menuId : menuIds) {
                 userRoleDao.insertRoleAndMenuId(role.getId(),menuId);

            }

        }
    }

    @Override
    public List<Menu> findAllMenu() {
        log.info("[角色-菜单项");

        return userRoleDao.selectAllMenu();
    }

    @Override
    public List<Menu> findMenuByRole(Integer id) {

        return userRoleDao.selectMenuByRole(id);
    }


    @Override
    public void edit(Role role, Integer[] permissionIds, Integer[] menuIds) {
        log.info("[角色-编辑]data:{},permissionIds:{},menuIds:{}");

        //更新角色列表
        userRoleDao.update(role);
        //2、更新关联关系
        //2.1、删除原有关系
        userRoleDao.deleteRoleAndPermissionByRoleIds(role.getId());

        //更新角色菜单表
        userRoleDao.deleteRoleAndMenuByRoleIds(role.getId());


        for (Integer permissionId : permissionIds) {
            userRoleDao.insertRoleAndPermissionId(role.getId(),permissionId);

        }

        for (Integer menuId : menuIds) {
            userRoleDao.insertRoleAndMenuId(role.getId(),menuId);
        }
    }

    @Override
    public List<Permission> findPermissionByRole(Integer id) {

        return userRoleDao.selectPermissionByRole(id);
    }

    @Override
    public List<Menu> findMenuByRoleId(Integer id) {

        return userRoleDao.selectMenuByRoleId(id);
    }

    @Override
    public List<Permission> findPermissionIdByRoleId(Integer id) {


        return userRoleDao.selectPermissionIdByRoleId(id);
    }

    @Override
    public void delete(Integer id) {
        log.info("[权限-删除]data:{}",id);

        Long menucount = userRoleDao.countMenuIdByRoleId(id);

        Long permissioncount = userRoleDao.countPermissionIdByRoleId(id);
        if (menucount>0 || permissioncount>0){
            throw new BusinessRuntimeException("数据有关联，不能删除");
        }

        userRoleDao.delete(id);
    }


}